<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8" />
		<title>JsonRpc 协议测试</title>
	</head>
	<body>
		<!--
		<p>
        <button onclick="testRpc4WebHost()">testRpc4WebHost</button>
        <button onclick="testRpc4Http()">testRpc4Http</button>
        <button onclick="testRpc4WebSocket()">testRpc4WebSocket</button>
        <button onclick="testRpc4Mqtt()">testRpc4Mqtt</button>
        </p>
		-->
        <p>
        <button onclick="testAgent4WebHost()">testAgent4WebHost</button>
        <button onclick="testAgent4Http()">testAgent4Http</button>
        <button onclick="testAgent4WebSocket()">testAgent4WebSocket</button>
        <button onclick="testAgent4Mqtt()">testAgent4Mqtt</button>
        </p>
        <p>
        <button onclick="testGa4WebHost()">testGa4WebHost</button>
        <button onclick="testGa4Http()">testGa4Http</button>
        <button onclick="testGa4WebSocket()">testGa4WebSocket</button>
        <button onclick="testGa4Mqtt()">testGa4Mqtt</button>
        </p>
		<p>
		<a onclick="txtlog.value=''">清除日志</a>
		</p>
		<p><textarea id="txtlog" rows="65" cols="80" readonly="true"></textarea><p>

        <script src="/common/js/utils.js"></script>
        <script src="/common/js/paho-mqtt-1.0.1.js"></script>
        <script src="/common/js/wskt.js"></script>
        <script src="/common/js/jsonrpc.js"></script>
        <script src="/common/js/jsonrpc-agent.js"></script>
        <script src="/common/js/gateway-agent.js"></script>

		<script>
			// 日志输出到页面上，以便调试
			window.console = {};
			window.console.log = function(message) {
				// 记住当前选择，以便添加完成之后恢复它；否则会导致用户的选择被替换
				let [start, end] = [txtlog.selectionStart, txtlog.selectionEnd];
				// 选中到末尾
				txtlog.setSelectionRange(txtlog.textLength, txtlog.textLength);
				// 替换选择为要添加的信息
				txtlog.setRangeText(message + '\r\n');
				// 恢复选择
				txtlog.setSelectionRange(start, end);
				// 自动滚动到最后
				txtlog.scrollTop = txtlog.scrollHeight;
				txtlog.focus();

				// 如果运行在内置浏览器，则挂接日志输出
				if(window.hasOwnProperty("webHost")) {
					webHost.log(message);
				}
			};

			// 错误输出到页面上，以便调试
			window.onerror = function(message, source, lineno, colno, error) {
				console.log("error: " + source + "#" + lineno + "." + colno + "\r\n\t" + message);
				// 如果运行在内置浏览器，则挂接错误输出
				if(window.hasOwnProperty("webHost")) {
					webHost.error(message, source, lineno, colno, error);
				}
			}

			window.onload = function() {
				if(window.hasOwnProperty("webHost")) {
					console.log("webHost.isReady() = " + webHost.isReady());
				}
			}

		    console.log(navigator.userAgent);
            console.log("screenSize: " + screen.width + "x" + screen.height);
			console.log("document.bodySize: " + document.body.clientWidth + "x" + document.body.clientHeight);
            console.log("innerSize: " + window.innerWidth + "x" + window.innerHeight);
			txtlog.cols = document.body.clientWidth / 7.05;
			txtlog.rows = (window.innerHeight - 130) / 14.1;
			console.log("txtlog.size:" + txtlog.cols + "x" + txtlog.rows);

            function onRpcResult(data) {
            	console.log(JSON.stringify(data));
            }

			function onRpcNotify(event) {
            	console.log("收到事件：" + event.params);
			}

			// 测试原始 JsonRpc 连接
            var rpc4WebHost = new JsonRpc("webhost@webHost");
            var rpc4Http = new JsonRpc("http@http");
            var rpc4WebSocket = new JsonRpc("wskt@ws://localhost:8504/webskt");
            var rpc4Mqtt = new JsonRpc("mqtt@ehome/gateway4/mdm/up/#wss://ehome.szwistar.com/mqtt/");

            function testRpc4WebHost() {
                console.log("testRpc4WebHost() ...")
				rpc4WebHost.connect();
                rpc4WebHost.rpc(null, null, "app.localLogin", {userName: "awander", password: "123456"}, onRpcResult, 10000);
            }

            function testRpc4Http() {
                console.log("testRpc4Http() ...")
				rpc4Http.connect();
                rpc4Http.rpc("http://localhost:9404/jsonRpc/call", null, "app.localLogin", {userName: "awander", password: "123456"}, onRpcResult, 10000);
            }

            function testRpc4WebSocket() {
                console.log("testRpc4WebSocket() ...")
				rpc4WebSocket.connect();
                rpc4WebSocket.rpc(null, null, "app.localLogin", {userName: "awander", password: "123456"}, onRpcResult, 10000);
            }

            function testRpc4Mqtt() {
                console.log("testRpc4Mqtt() ...")
				rpc4Mqtt.connect();
                rpc4Mqtt.rpc("ehome/gateway4/mdm/down/3E00FFFF00000002", null, "app.localLogin", {userName: "awander", password: "123456"}, onRpcResult, 10000);
            }

			// 测试 JsonRpcAgent (重用前面已经创建的连接)
			const token = "5ecf78e549c87000";
            var agent4WebHost   = new JsonRpcAgent(rpc4WebHost, null, token);
            var agent4WebSocket = new JsonRpcAgent(rpc4WebSocket, null, token);
            var agent4Http      = new JsonRpcAgent(rpc4Http, "http://localhost:9404/jsonRpc/call", token);
            var agent4Mqtt      = new JsonRpcAgent(rpc4Mqtt, "ehome/gateway4/mdm/down/3E00FFFF00000002", token);

			async function testAgent(agent) {
				// 测试 RPC
                let data = await agent.rpc("obj.getConfig", {oid: 89}, 5000);
				onRpcResult(data);
				// 测试消息订阅与通知处理
				agent.registerNotifyHandler("handleEvent", onRpcNotify);
				data = await agent.subscribeEx(["ehome/gateway4/mdm/report/3E00FFFF00000002/#"]);
				console.log("消息订阅返回："); onRpcResult(data);
			}

            async function testAgent4WebHost() {
                console.log("testAgent4WebHost() ...")
				testAgent(agent4WebHost);
            }

            async function testAgent4WebSocket() {
                console.log("testAgent4WebSocket() ...")
				testAgent(agent4WebSocket);
            }

            async function testAgent4Http() {
                console.log("testAgent4Http() ...")
				testAgent(agent4Http);
            }

            async function testAgent4Mqtt() {
                console.log("testAgent4Mqtt() ...")
				testAgent(agent4Mqtt);
            }

			// 测试 GatewayAgent
			var gatewayInfo = {
				id: "3E00FFFF00000002",
				token: token,
			};
			var ga4WebHost   = new GatewayAgent(rpc4WebHost, null, gatewayInfo);
			var ga4WebSocket = new GatewayAgent(rpc4WebSocket, null, gatewayInfo);
			var ga4Http      = new GatewayAgent(rpc4Http, "http://localhost:9404/jsonRpc/call", gatewayInfo);
			var ga4Mqtt      = new GatewayAgent(rpc4Mqtt, "ehome/gateway4/mdm/down/3E00FFFF00000002", gatewayInfo);

			async function testGatewayAgent(agent) {
				// 测试 RPC
                let data = await agent.getObjConfig(0);
				onRpcResult(data);
				// 订阅上报事件
				data = await agent.subscribeEx(["ehome/gateway4/mdm/report/3E00FFFF00000002/0/65/#"]);
				console.log("消息订阅返回："); onRpcResult(data);
			}

			async function testGa4WebHost() {
                console.log("testGa4WebHost() ...")
				testGatewayAgent(ga4WebHost);
			}

			async function testGa4WebSocket() {
                console.log("testGa4WebSocket() ...")
				testGatewayAgent(ga4WebSocket);
			}

			async function testGa4Http() {
                console.log("testGa4Http() ...")
				testGatewayAgent(ga4Http);
			}

			async function testGa4Mqtt() {
                console.log("testGa4Mqtt() ...")
				testGatewayAgent(ga4Mqtt);
			}
		</script>
	</body>

	<style>
		* {
			font-family: Consolas;
			font-size: 12px;
		}
	</style>
</html>
